package com.peter.aboot.controller;

import com.peter.aboot.module.Book;
import com.peter.aboot.repository.ReadingListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author jiaoshuai.js
 */
@RestController
@RequestMapping("/")
public class ReadingListController {

  @Autowired
  ReadingListRepository readingListRepository;

  @GetMapping("/{reader}")
  public List<Book> readersBooks(@PathVariable("reader") String reader, Model model){
    return
    Optional.ofNullable(readingListRepository.findByReader(reader))
      .orElse(null);
  }

  @PostMapping("/{reader}")
  public String addToReadList(@PathVariable("reader") String reader, Book book){

    book.setReader(reader);
    readingListRepository.save(book);
    return "redirect:/{reader}";

  }


}
